﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Cloudix.Common
{

    public sealed class CloudixScriptManager
    {

        public static CloudixScriptManagerBehaviour Behaviour 
        {
            get { return SessionManager.Retrieve<CloudixScriptManagerBehaviour>("contentBehaviour"); }
            private set { SessionManager.Add<CloudixScriptManagerBehaviour>("contentBehaviour", value); }
        }

        public static void RegisterScriptManager(IContentProvider customProvider, ContentEditMode editMode, Languages language = Languages.DefaultOrUnspecified)
        {
            RegisterScriptManager(new CloudixScriptManagerBehaviour(customProvider, editMode, language));
        }

        public static void RegisterScriptManager(CloudixScriptManagerBehaviour behaviour)
        {
            // Set the behaviour
            Behaviour = behaviour;
        }

        public static void RegisterClientSideIncludes()
        {
            StringBuilder htmlContent = new StringBuilder();

            // TODO: Let's make this... decent. 
            // This is a poor man's solution.

            if (Behaviour != null && Behaviour.EditMode == ContentEditMode.Editable)
            {
                // We verified the ContentMode is set to be editable, so we therefor
                // start registering the neccesary includes like css and javascript.

                // Embed Microsoft AJAX CDN
                htmlContent.Append(@"<script src=""http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js"" type=""text/javascript""></script>");

                // Embed Google jQuery UI CDN
                htmlContent.Append(@"<script src=""http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js"" type=""text/javascript""></script>");
                htmlContent.Append(@"<link rel=""stylesheet"" type=""text/css"" href=""http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/themes/smoothness/jquery-ui.css"" />");

                // Read styles from embedded resource
                htmlContent.Append(@"<style type=""text/css"">");
                htmlContent.Append(ResourceHelper.FetchStyles());
                htmlContent.Append("</style>\n");

                // Read scripts from embedded resource
                htmlContent.Append(@"<script type=""text/javascript"">");
                htmlContent.Append(ResourceHelper.FetchScripts());
                htmlContent.Append("</script>\n");

            }
            else
            {
                // The content is not editable, so display some shameless promotion as a meta tag
                htmlContent.Append(@"<meta name=""generator"" content=""Cloudix | http://cloudix.org"">");
            }

            // Render the output as Html
            HttpContext.Current.Response.Write(htmlContent.ToString());

        }       

        /// <summary>
        /// Returns the basic literal value without any (Html) markup.
        /// </summary>
        public static string RenderRawContent(Guid contentId)
        {
            try
            {
                return Behaviour.Provider.LoadContent(contentId, Behaviour.Language);
            }
            catch (Exception)
            {
                // TODO: We should catch exceptions more decently here...
                throw;
            }
        }

        /// <summary>
        /// Renders the value with Cloudix' default Html markup and injects it into the HttpResponse.
        /// </summary>
        public static void RenderHtmlContent(Guid contentId)
        {
            StringBuilder htmlContent = new StringBuilder();

            // Content is, by default, fetched through the ContentProvider
            string content = RenderRawContent(contentId);

            // Depending on the provided EditMode, we display the content in different ways
            if (Behaviour.EditMode == ContentEditMode.ReadOnly)
            {
                htmlContent.Append(String.Format("<div>{0}</div>", content));
            }
            else
            {
                htmlContent.Append(String.Format("<div id=\"{1}\" class=\"editable\">{0}</div>", 
                    content, contentId));
            }

            // Render the output as Html
            HttpContext.Current.Response.Write(htmlContent.ToString());
        }

    }
}
